# -*- coding: utf-8 -*-
from django.core.exceptions import PermissionDenied
from django.http import Http404
from django.shortcuts import get_object_or_404
from django.views.generic import ListView
from django.views.generic.detail import DetailView
from django.views.generic.edit import CreateView, UpdateView, DeleteView
from news.forms import CreateForm, CFormCreate
from news.models import Category
from oets.news.models import News


class NewsByCategory(ListView):
    template_name = 'news/news_list.html'
    paginate_by = 5
    
    def get_queryset(self):
        cat = get_object_or_404(Category, slug__iexact=self.args[0])
        return News.objects.filter(category=cat, enabled=True).order_by('-date')

class NewsList(ListView):
    template_name = 'news/news_list.html'
    queryset = News.objects.filter(enabled=True).order_by('-date')
    paginate_by = 5
        

class NewsDetail(DetailView):
    template_name = 'news/news_detail.html'
    def get_queryset(self):
        cat = get_object_or_404(Category, slug__iexact=self.kwargs.get('arg', None))
        return News.objects.filter(category=cat)


class NewsCreateView(CreateView):
    template_name = 'news/news_create.html'
    success_url = '/news/'
    form_class = CreateForm
#    model = News

    def get_form_kwargs(self):
        kwargs = super(NewsCreateView, self).get_form_kwargs()
        kwargs.update({'initial': {'user':self.request.user}}) # Добавляем в список аргументов, юзера
        return kwargs

    def get(self, request, *args, **kwargs):
        if request.user.has_perm('news.add_news'):
            if not Category.objects.filter(operator=request.user).count()\
                and not request.user.is_superuser:
                raise PermissionDenied
        else:
            raise PermissionDenied
        return super(NewsCreateView, self).get(request, *args, **kwargs)
    
class NewsUpdateView(UpdateView):
    form_class = CreateForm
    template_name = 'news/news_update.html'
    success_url = '/news/'
    queryset = News.objects.filter(enabled=True)

    def get(self, request, *args, **kwargs):
        context = super(NewsUpdateView, self).get(request, *args, **kwargs)
        if request.user.has_perm('news.add_news'):
            if not request.user in self.object.category.operator.all() and not request.user.is_superuser:
                raise PermissionDenied
        else:
            raise PermissionDenied
        return context


    def get_form_kwargs(self):
        kwargs = super(NewsUpdateView, self).get_form_kwargs()
        kwargs.update({'initial': {'user':self.request.user}}) # Добавляем в список аргументов, юзера
        return kwargs

class NewsDeleteView(DeleteView):
    success_url = '/news/'
    queryset = News.objects.all()

    def get(self, request, *args, **kwargs):
        context = super(NewsDeleteView, self).get(request, *args, **kwargs)
        if request.user.has_perm('news.delete_news'):
            if not request.user in self.object.category.operator.all() and not request.user.is_superuser:
                raise PermissionDenied
        else:
            raise PermissionDenied
        return context

class CategoryDeleteView(DeleteView):
    success_url = '/news/catlist/'
    queryset = Category.objects.all()

    def get(self, request, *args, **kwargs):
        context = super(CategoryDeleteView, self).get(request, *args, **kwargs)
        if request.user.is_superuser:
            return context
        raise PermissionDenied

    
class CategoryCreate(CreateView):
    form_class = CFormCreate
    template_name = 'news/category_create.html'
    success_url = '/news/catlist/'

    def get(self, request, *args, **kwargs):
        context = super(CategoryCreate, self).get(request, *args, **kwargs)
        if not request.user.is_superuser:
                raise PermissionDenied
        return context

class CategoryUpdate(UpdateView):
    form_class = CFormCreate
    template_name = 'news/category_create.html'
    success_url = '/news/catlist/'
    queryset = Category.objects.all()

    def get(self, request, *args, **kwargs):
        context = super(CategoryUpdate, self).get(request, *args, **kwargs)
        if not request.user.is_superuser:
                raise PermissionDenied
        return context

class CategoryList(ListView):
    template_name = 'news/cat_list.html'
    queryset = Category.objects.all()

    def get(self, request, *args, **kwargs):
        context = super(CategoryList, self).get(request, *args, **kwargs)
        if not request.user.is_superuser:
                raise PermissionDenied
        return context